package mh.demo.jdk.time;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * Instant代表一个时间点，这与Date类似
 */
public class InstantDemo {

    /**
     * 返回当前系统的UTC时间
     */
    public Instant nowInstant() {
        return Instant.now();
    }

    public Date toDate(Instant instant) {
        return Date.from(instant);
    }

    public Instant toInstant(Date date) {
        return Instant.ofEpochMilli(date.getTime());
    }

    public ZonedDateTime toZonedDateTime(Instant instant) {
        return instant.atZone(ZoneId.systemDefault());
    }

    public LocalDateTime toLocalDateTime(Instant instant) {
        return toZonedDateTime(instant).toLocalDateTime();
    }

}
